/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#include "logwatcher.h"

TailWatch::TailWatch(std::string p_filename, QTextEdit *document):
	m_filename(p_filename),
	m_file(NULL),
	m_document(document) {

	m_file = new std::ifstream(filename().c_str());
	pos = m_file->tellg();
	m_watcher = new QFileSystemWatcher(this);
	m_watcher->addPath(QString(filename().c_str()));
	connect(m_watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(updateDocument()));
}

TailWatch::~TailWatch() {
	DELETE(m_file);
}

void TailWatch::updateDocument() {
	// Read new lines from file and append to document
	std::string line;
	m_file->clear();
	while (getline(*m_file, line)) {
		document()->append(QString(line.c_str()));
	}
}
